import fitz

def read_and_write_pdf(file_path, output_text_file):
    # 打开 PDF 文件
    pdf_document = fitz.open(file_path)

    # 获取 PDF 中的页数
    num_pages = pdf_document.page_count

    # 打开文本文件进行写入
    with open(output_text_file, 'w', encoding='utf-8') as output_file:
        # 读取每一页的文本并写入文本文件
        for page_num in range(num_pages):
            page = pdf_document[page_num]
            text = page.get_text()
            output_file.write(f"Page {page_num + 1}:\n{text}\n\n")

    # 关闭 PDF 文件
    pdf_document.close()

if __name__ == "__main__":
    pdf_file_path = "F://tmp//遥远的救世主.pdf"
    output_text_file = "F://tmp//output_遥远的救世主.txt"
    read_and_write_pdf(pdf_file_path, output_text_file)
